//
//  StatusCell.m
//  weibo110
//
//  Created by 臧松 on 15/12/31.
//  Copyright © 2015年 臧松. All rights reserved.
//
#define StatusCellMargin 12
#define StatusCellIconWidth 35

#import "StatusCell.h"
#import "StatusTopView.h"
#import "StatusBottomView.h"
#import "UILabel+Extension.h"
#import "Masonry.h"
#import "StatusPictureView.h"
@interface StatusCell ()
// 顶部视图
@property (nonatomic,strong) StatusTopView * topView;


@end

@implementation StatusCell
// 重写 set 方法 赋值控件
-(void)setStatusViewModel:(StatusViewModel *)statusViewModel{
    
    _statusViewModel = statusViewModel;
    
    self.topView.statusViewModel = statusViewModel;
    self.pictureView.statusViewModel = statusViewModel;
    
    
    self.contentLabel.text = statusViewModel.status.text;
    [self.pictureView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(@(self.pictureView.bounds.size.height));
        make.width.equalTo(@(self.pictureView.bounds.size.width));

    }];
    
    
}
// 懒加载控件
-(StatusTopView *)topView{
    if (_topView == nil) {
        
        _topView = [[StatusTopView alloc]init];
    }
    return _topView;
}
-(UILabel *)contentLabel{
    if (_contentLabel == nil) {
        _contentLabel = [UILabel labelWithTitle:@"微博正文" fontSize:14 color:[UIColor darkGrayColor] screenInset:StatusCellMargin];
    }
    return _contentLabel;
}
-(StatusBottomView *)bottomView{
    if (_bottomView == nil) {
        _bottomView = [[StatusBottomView alloc]init];
    }
    return _bottomView;
}
-(StatusPictureView *)pictureView{
    if (_pictureView == nil) {
        _pictureView = [[StatusPictureView alloc]init];
    }
    return _pictureView;
}
// 计算行高的方法
-(CGFloat)rowHeightWithModel:(StatusViewModel *)statusViewModel{
    self.statusViewModel = statusViewModel;
    [self.contentView layoutIfNeeded];
    
    return CGRectGetMaxY(self.bottomView.frame);

}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

//MARK: - 设置界面
-(void)setupUI{
    
    // 添加控件
    
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.pictureView];
    [self.contentView addSubview:self.bottomView];
    
    
    
    // 自动布局
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);

        make.height.equalTo(@(StatusCellIconWidth + 2 * StatusCellMargin));
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(StatusCellMargin);
        make.left.equalTo(self.contentView.mas_left).offset(StatusCellMargin);
        
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pictureView.mas_bottom).offset(StatusCellMargin);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.height.equalTo(@44);
        
    }];
    
}

@end
